<?php
require_once dirname(__FILE__).'/Autoloader/LyAutoloader.class.php';

class AppUtil {
	static $context;
	static $instances;
	static $config;
	static $autoloader;
	static function initApp($autoDirs,$cacheDir,$configDir,$dev = false,$autoloadType = 'include'){
		if(!isset(self::$autoloader)){
			$autoloader = new LyAutoloader();
			$autoloader->setCacheDir($cacheDir);
			$autoloader->addAutoDirs($autoDirs);
			$autoloader->scanFiles($dev);
			$autoloader->beginAutoload($autoloadType);
			self::$instances['lyautoloader'] = $autoloader;
			self::$autoloader = $autoloader;
		}
		if(!isset(self::$config)){
			self::$config = new LyConfig();
			self::$instances['lyconfig'] = self::$config;
			self::$config->setCacheDir($cacheDir);
			self::$config->setConfigDir($configDir);
			self::$config->parseConfig($dev);

		}
	}
	static function signton($className){
		$className = strtolower(trim($className));
		if(!isset(self::$instances[$className])){
			$instance = new $className();
			$options = self::$config->getConfig($className);
			if($options && method_exists($instance,'setOptions')){
				$instance->setOptions($options);
			}
			self::$instances[$className] = $instance;
		}
		return self::$instances[$className];
	}
	static function runApp(){
		$router = self::signton('LyRouter');
		$router->init();
		$action = $router->getAction();
		$params = $router->getParams();
		$dispatcher = self::signton('LyDispatcher');
		$dispatcher->dispatchAction($action,$params);
	}
	static function config($name){

	}
	static function context($name,$value=''){

	}
}